<?php

/**
 * Subclass for performing query and update operations on the 'user' table.
 *
 *
 *
 * @package lib.model
 */
class UserPeer extends BaseUserPeer
{

	static public function getOppositesForSignature(LypoSignature $signature, $limit=10){
		$opposite_sig = $signature->getOppositeSignature();
		$num_answers = $opposite_sig->getNumAnswers();
		$c = new Criteria();
		$c->addAsColumn('score', sprintf("MATCH(%s) AGAINST('%s' IN BOOLEAN MODE)",UserPeer::LYPO_SIGNATURE,$opposite_sig->getSignature()));
		$c->addAsColumn('num_answers', $num_answers);
		$c->addAsColumn('compared_with', "'".$signature->getSignature()."'");
		$c->addDescendingOrderByColumn('score');
		$opposites = OppositePeer::doSelectAsOpposites($c);
		return $opposites;
	}

	static public function getOppositesCriteria($user, $criteria=null){
		$user_signature = new LypoSignature($user->getLypoSignature());
		$opposite_signature = $user_signature->getOppositeSignature();
		$num_answers = $opposite_signature->getNumAnswers();
		$c = ($criteria instanceof Criteria) ? clone $c : new Criteria();
		$c->addAsColumn('score', sprintf("MATCH(%s) AGAINST('%s' IN BOOLEAN MODE)",UserPeer::LYPO_SIGNATURE,$opposite_signature->getSignature()));
		$c->addAsColumn('num_answers', $num_answers);
		$c->addAsColumn('compared_with', "'".$user_signature->getSignature()."'");
		$c->addDescendingOrderByColumn('score');
		return $c;
	}

	static public function getPerfectOppositesCriteria($user, $criteria=null){
		$c = self::getOppositesCriteria($user,$criteria);
		$c->add(PerfectOppositePeer::USER_ID,$user->getId());
		$c->addJoin(UserPeer::ID,PerfectOppositePeer::OPPOSITE_ID);
		return $c;
	}
	
	static public function getReversedPerfectOppositesCriteria(User $user, $criteria=null){
		$c = new Criteria();
		$c->add(PerfectOppositePeer::OPPOSITE_ID,$user->getId());
		$c->addJoin(UserPeer::ID,PerfectOppositePeer::USER_ID);
		return $c;
	}

	
	static public function getUsersWhoRespect(User $user){
		$c = new Criteria();
		$c->add(ReactionPeer::USER_ID,$user->getId());
		$c->addJoin(ReactionPeer::ID,ReactionRespectPeer::REACTION_ID);
		$c->addJoin(UserPeer::ID,ReactionRespectPeer::USER_ID);
		$c->addAsColumn('numrespects','COUNT(*)');
		$c->addGroupByColumn(ReactionRespectPeer::USER_ID);
		$rs = UserPeer::doSelectRS($c);
		
		$respects = array();
		$results = array();
		$cls = UserPeer::getOMClass();
		$cls = sfPropel::import($cls);
		while($rs->next()) {
			$obj = new $cls();
			$offset = $obj->hydrate($rs);
			$respects[$obj->getId()]=$rs->getInt($offset);
			$results[] = $obj;
		}
		$data = array('users' => $results, 'respects' => $respects);
		return $data;
	}

	static public function updateLypoSignatureForUser(User $user){
		$c = new Criteria();
		$c->add(StatementAnswerPeer::USER_ID,$user->getId());
		$c->addJoin(LypoStatementPeer::ID,StatementAnswerPeer::STATEMENT_ID);
		$c->addAscendingOrderByColumn(StatementAnswerPeer::STATEMENT_ID);
		$answers = StatementAnswerPeer::doSelect($c);
		if($answers){
			$sig = array();
			foreach($answers as $answer){
				$sig[]=sprintf("%03s%s",$answer->getStatementId(),$answer->getAnswer());
			}
			$signature = join(" ",$sig);
			$c = new Criteria();
			$c->add(UserPeer::ID,$user->getId());
			$c->add(UserPeer::LYPO_SIGNATURE,$signature);
			UserPeer::doUpdate($c);
			$user->setLypoSignature($signature);
		}

	}

	static public function getByContainerString($userid){
		try {
			$osUser = new OpenSocialContainerUser($userid);
			//Okee, so far so good, nu gebruiker ophalen
			$c = new Criteria();
			$c->add(ContainerPeer::OPENSOCIAL_NAME,$osUser->getContainerId());
			$c->add(UserContainerPeer::CONTAINER_USERID, $osUser->getId());
			$c->addJoin(UserPeer::ID,UserContainerPeer::USER_ID);
			$c->addJoin(UserContainerPeer::CONTAINER_ID, ContainerPeer::ID);
			return UserPeer::doSelectOne($c);
		}catch(Exception $e){
			throw $e;
		}

	}


}
